﻿using System;
using System.Collections.Generic;
using System.Text;
using WebApi.Service.Dto;
using WebApi.Repository;
using System.Linq;
using WebApi.Service.Other;

namespace WebApi.Service.Impl
{
    public class TagService:ITagService
    {
        private readonly CommodityDbContext commodityDbContext;
        public TagService(CommodityDbContext commodityDbContext)
        {
            this.commodityDbContext = commodityDbContext;
        }

        /// <summary>
        /// 新增标签
        /// </summary>
        /// <param name="name">标签的名称</param>
        /// <returns></returns>
        public ResponseResult<bool> AddNewTag(string name)
        {
            //若传入的name为null或空，返回false
            if (String.IsNullOrEmpty(name))
                return new ResponseResult<bool>()
                {
                    State = 2,
                    Data = false,
                    Message = "输入的名称不能为空"
                };
            //若传入的name长度大于100，返回false
            if (name.Length > 100)
                return new ResponseResult<bool>()
                {
                    State = 2,
                    Data = false,
                    Message = "输入的名称长度不能大于100"
                };
            //查看数据库中是否有相同名称且未删除的标签，若有，返回false
            var entity = commodityDbContext.Tags.Where(o => o.Name == name).FirstOrDefault();
            if (entity != null) 
            {
                return new ResponseResult<bool>()
                {
                    State = 2,
                    Data = false,
                    Message = "已存在同名标签，无法新增"
                };
            }
            //插入相应的标签
            commodityDbContext.Tags.Add(new TagEntity
            {
                Name = name
            });
            var changes = commodityDbContext.SaveChanges() > 0 ;
            return new ResponseResult<bool>()
            {
                State = changes ? 1 : 3,
                Message = changes ? "插入成功" : "插入失败",
                Data = changes
            };
        }
        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="id">标签的id</param>
        /// <returns></returns>
        public ResponseResult<bool> DeleteTag(List<long>tagsId)
        {
            foreach(long id in tagsId)
            {
                var entity = commodityDbContext.Tags.Where(o => o.Id == id).FirstOrDefault();
                //若没有相应的标签，返回错误信息
                if (entity == null)
                {
                    return new ResponseResult<bool>()
                    {
                        State = 3,
                        Data = false,
                        Message = $"删除的id为{id}的标签不存在"
                    };
                }
                //删除位设置为true
                entity.IsDeleted = true;

            }
            var changes = commodityDbContext.SaveChanges() > 0;
            return new ResponseResult<bool>()
            {
                State = changes ? 1 : 3,
                Message = changes ? "删除成功" : "删除失败，改标签已被删除",
                Data = changes
            };
        }
        /// <summary>
        /// 更新标签
        /// </summary>
        /// <param name="tag">传入的要修改的标签</param>
        /// <returns>是否成功修改</returns>
        public ResponseResult<bool> UpdateTag(TagDto tag)
        {
            var entity = commodityDbContext.Tags.Where(o => o.Id == tag.Id).FirstOrDefault();
            if(entity==null)
            {
                return new ResponseResult<bool>()
                {
                    State = 3,
                    Data = false,
                    Message = "修改的标签不存在"
                };
            }
            //名称为空或null，返回false
            if (String.IsNullOrEmpty(tag.Name))
                return new ResponseResult<bool>()
                {
                    State = 2,
                    Data = false,
                    Message = "名称不能为空"
                };
            //没有修改名称，直接返回true
            if (entity.Name == tag.Name)
                return new ResponseResult<bool>()
                {
                    State = 1,
                    Data = true,
                    Message="修改成功"
                };
            //修改的名称重名，返回false
            var entities = commodityDbContext.Tags.Where(o => o.Name == tag.Name).ToList();
            if (entities.Count != 0)
                return new ResponseResult<bool>()
                {
                    State = 2,
                    Data = false,
                    Message = "标签名已存在"
                };
            //名称长度大于100，返回false
            if (tag.Name.Length > 100)
                return new ResponseResult<bool>()
                {
                    State = 2,
                    Data = false,
                    Message = "标签名长度不能大于100"
                };
            //修改名字、保存
            entity.Name = tag.Name;
            var changes = commodityDbContext.SaveChanges() > 0;
            return new ResponseResult<bool>()
            {
                State = changes ? 1 : 3,
                Message = changes ? "修改成功" : "修改失败",
                Data = changes
            };
        }
        /// <summary>
        /// 查询所有没有被删除的标签，不分页
        /// </summary>
        /// <returns>获取的相应标签集</returns>
        public ResponseResult<List<TagDto>>GetTags()
        {
            var data = commodityDbContext.Tags.Where(o=>o.IsDeleted==false)
                .Select(o => new TagDto
            {
                Id = o.Id,
                Name=o.Name
            }).ToList();
            return new ResponseResult<List<TagDto>>()
            {
                Data = data,
                State = 1,
                Message="获取成功"
            };
        }
    }
}
